#include "LocalPlayer.h"
//#include "ui_VideoSender.h"


#include <stdio.h>                                                         
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>


//#define DEBUG_VIDEO_SENDER_
#define TEST_VIDEO_SENDER_

//Houlc Qt 

LocalPlayer::LocalPlayer(QWidget *parent)
    : QWidget(parent)
{
    qDebug()<<"This is qDebug";

    if(InitIniHandler("./etc/config.ini")==false)
        qDebug()<<"ini file read failed!";
    else
        qDebug()<<"ini file read succeeded!";

    update_interval_ms = GetIntegerKeyIni("Video", "update_interval_ms", 100);
    if(update_interval_ms == 0){
        qDebug()<<"update_interval_ms = 0, wrong ,edit to 100";
        update_interval_ms = 100;
    }
    qDebug()<<"update_interval_ms:"<<update_interval_ms;

    picture_input_dir = GetStringValueKeyIni("PictureInput",
                                             "picture_input_dir", "../picture");

    int wait_time = GetIntegerKeyIni("PicCache", "piccache_wait_time", 20);



    //init param
    playFlag = false;
    updateCount = 0;

    //init timer 
    //ui-> label_image->setScaledContents(true);//可以使图片完全按QWidget缩放，而不保持原视频比例
    videoTimer = new QTimer(this);
    connect(videoTimer, SIGNAL(timeout()), this, SLOT(imgUpdate()));

    msgTimer = new QTimer(this);
    connect(msgTimer, SIGNAL(timeout()), this, SLOT(msgUpdate()));

    dir = new QDir(picture_input_dir.c_str());
    if(!dir->exists())qWarning("cannot open dir");
    else qWarning("pic dir open!");

    picCache = new PicCache(dir, wait_time);



    //TODO
    //init widget
    this->resize(1200,1200);
    this->setWindowTitle(QString::fromUtf8("LocalPlayer"));
    
    
    qDebug()<<"add playerwidget";
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    this->playerWidget = new PlayerWidget(this);
    this->playerWidget->stopPlay();
    
    this->playerWidget->setLockLastImg(true);

    mainLayout->addWidget(playerWidget);

    qDebug()<<"add init pic";
    //QPixmap pp1, pp2, pp3;
    this->show_backimg=false;
    //this->backimg.load("./picture/StartPic/start0.jpg");
    this->backimg.load("./img/backimg.png");
    //this->playerWidget->pushPixmap2Show(pp1);
    //this->playerWidget->pushPixmap2Show(pp2);
    //this->playerWidget->pushPixmap2Show(pp3);


    qDebug()<<"add Slider";
    this->timeSlider = new QSlider(this);
    this->timeSlider->setOrientation(Qt::Horizontal);

    // 连接信号和槽
    connect(this->timeSlider, 
            //SIGNAL(valueChanged(int)), 
            SIGNAL(sliderMoved(int)), 
            this, SLOT(on_timeSlider_sliderMoved(int))); 



    pp_play.load("./img/play.png");
    pp_pause.load("./img/pause.png");
    
    qDebug()<<"add PushButton";
    //this->playButton = new QPushButton("PLAY", this);
    this->playButton = new QPushButton( this);
    connect(playButton, &QPushButton::clicked,
            this, [this](){
                this->on_pushButton_play_clicked();
            });
    this->playButton->setIcon(QIcon(pp_play));


    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(this->timeSlider);
    buttonLayout->addWidget(this->playButton);

    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);
    std::cout<<"LocalPlayer-init-end"<<std::endl;

    qDebug()<<"dfault init";
    //default init
    this->init();

    //this->playButton->setIcon(QIcon(pp_pause));
    //this->playerWidget->startPlay(1000/update_interval_ms);
    //this->videoTimer->start(update_interval_ms);
    //this->msgTimer->start(update_interval_ms);
    //playFlag = true;

}

void LocalPlayer::init() {
    this->picCache->start();

    //show init back img
    this->playerWidget->startPlay(1000/update_interval_ms);

    if(show_backimg == false){
        this->playerWidget->pushPixmap2Show(backimg);
        show_backimg = true;
    }
}

void LocalPlayer::msgUpdate()
{
    if(picCache->picList->count() != 0)
        this->timeSlider->setValue(100.0*updateCount/picCache->picList->count());
}
void LocalPlayer::imgUpdate()
{
    //qWarning("imgUpdate");
    qDebug()<<"[imageUpdate]";
    qDebug()<<"picCache->picList->count():" << picCache->picList->count();
    if(picCache->picList->count()==0)
    {
        if(show_backimg == false){
            this->playerWidget->pushPixmap2Show(backimg);
            show_backimg = true;
        }
    }
    else if(updateCount < picCache->picList->count())
    {
        //刷新图像
        //ui->imgLabel->setPixmap(picCache->picList->at(updateCount).pixmap); 
        qDebug()<<"imagUpdate->pushPixmap2Show";
        this->playerWidget->pushPixmap2Show(picCache->picList->at(updateCount).pixmap);
        updateCount++;//刷新图像的编号，和list中的编号一一对应
    }
    else if(updateCount == picCache->picList->count())
    {
        qDebug()<<"imagUpdate->pushPixmap2Show";
        this->playerWidget->pushPixmap2Show(picCache->picList->at(updateCount-1).pixmap);
    }
    //else qDebug()<<"缓存慢于界面刷新";
}

LocalPlayer::~LocalPlayer()
{
    delete playerWidget;
    delete videoTimer;
    delete msgTimer;
    //delete ui;
}

void LocalPlayer::on_pushButton_play_clicked()
{
    qWarning("on_pushButton_play_clicked()");
    if(playFlag == true){
        this->playButton->setIcon(QIcon(pp_play));
        this->playerWidget->stopPlay();
        this->videoTimer->stop();
        this->msgTimer->stop();
        playFlag = false;
    }
    else if(playFlag == false){
        this->playButton->setIcon(QIcon(pp_pause));
        this->playerWidget->startPlay(1000/update_interval_ms);
        this->videoTimer->start(update_interval_ms);
        this->msgTimer->start(update_interval_ms);
        playFlag = true;
    }
}

void LocalPlayer::on_timeSlider_sliderMoved(int position) {
    qWarning("on_timeSlider_sliderMoved");
    updateCount = picCache->picList->count()/100.0*position;
    this->playButton->setIcon(QIcon(QPixmap(":/new/prefix1/img/play.png")));

    this->playButton->setIcon(QIcon(pp_play));
    this->playerWidget->stopPlay();
    this->videoTimer->stop();
    this->msgTimer->stop();
    playFlag = false;

    imgUpdate();
}


